# encoding: utf-8

from django.conf.urls.defaults import *
from django.contrib.auth.decorators import login_required
from django.contrib.auth.views import login as ologin
from django.contrib.auth.views import logout
from django.views.generic import list_detail
from django.contrib import databrowse
from django_restapi.model_resource import Collection
from django_restapi.responder import XMLResponder
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
import settings
import re
from netaddr import IPNetwork, IPAddress
from dns.feeds import LatestEntries
from dns.feeds import DomainFeed
from dns.feeds import AtomLatestEntries
from dns.feeds import AtomDomainFeed
from shared.views import printenv
import django_cron
django_cron.autodiscover()

def login(request):
	if not 'HTTP_X_FORWARDED_FOR' in request.META:
		ip=request.META['REMOTE_ADDR']
	else:
		ip=request.META['HTTP_X_FORWARDED_FOR']
		match=re.match('^::ffff:([0-9\.]+)$', ip, re.IGNORECASE)
		if match:
			ip=match.group(1)
	for item in settings.ADMIN_NETWORKS:
		cidr=IPNetwork(item)
		mip=IPAddress(ip)
		if mip in cidr:
			return ologin(request)
	return render_to_response('nologin.html',{'ip': ip},context_instance=RequestContext(request))

# RSS feeds
rss_feeds = {
	'latest': LatestEntries,
	'domain': DomainFeed,
}

# ATOM feeds
atom_feeds = {
	'latest': AtomLatestEntries,
	'domain': AtomDomainFeed,
}

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
	# admin interface
	(r'^admin/doc/', include('django.contrib.admindocs.urls')),
	(r'^admin/(.*)', admin.site.root),
	#(r'^admin/', include('django_docs.urls')),
	#(r'^.*printenv/$', printenv),
	# dns
	(r'^dns/', include('dns.urls')),
    # Example:
    # (r'^crs/', include('crs.foo.urls')),
	# databrowse
	#(r'^databrowse/(.*)', login_required(databrowse.site.root)),
	#accounts
	(r'^accounts/', include('registration.urls')),
	#profiles
	(r'^profiles/', include('profiles.urls')),
	# login
	(r'^accounts/login/$',  login),
	(r'^accounts/logout/$', login_required(logout)),
	# forum
	(r'^forum/', include('forum.urls')),
	# wiki
	(r'^wiki/', include('wiki.urls')),
	(r'^', include('wiki.static_urls')),
	# notification
	#(r'^notification/', include('notification.urls')),
  	# RSS feeds
	(r'^feeds/rss/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', {'feed_dict' : rss_feeds}),
	# ATOM feeds
	(r'^feeds/atom/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', {'feed_dict' : atom_feeds}),	
)
